package com.ycg.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycg.blog.common.PageView;
import com.ycg.blog.common.R;
import com.ycg.blog.pojo.Tag;
import com.ycg.blog.pojo.vo.SimpleArticleVo;
import com.ycg.blog.pojo.vo.TagVo;
import com.ycg.blog.service.TagService;
import com.ycg.blog.mapper.TagMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author lenovo
* @description 针对表【blog_tag(文章分类信息)】的数据库操作Service实现
* @createDate 2024-03-02 12:05:04
*/
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag>
    implements TagService{

    @Override
    public R<PageView<TagVo>> getTagList(Integer current, Integer pageSize) {
        Page<TagVo> page = new Page<>(current, pageSize);
        List<TagVo> tagVoList = getBaseMapper().getTags(page);
        PageView<TagVo> pageView = new PageView<>();
        pageView.setPageList(tagVoList);
        pageView.setTotal(page.getTotal());
        return R.success(pageView);
    }

    @Override
    public R<List<SimpleArticleVo>> getArticles(Integer tagId) {
        List<SimpleArticleVo> articleVos = getBaseMapper().getArticles(tagId);
        return R.success(articleVos);
    }

    @Override
    public R<String> updateTagName(Integer tagId, String newName) {
        LambdaUpdateWrapper<Tag> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Tag::getName,newName)
                .eq(Tag::getId,tagId);
        update(wrapper);
        return R.success("修改标签名称成功");
    }

    @Override
    public R<String> deleteTagById(Integer tagId) {
        getBaseMapper().deleteTagById(tagId);
        return R.success("删除成功");
    }

    /**
     * 根据标签名称新增或修改表情
     *
     * @param tags
     */
    @Override
    public void saveByName(List<Tag> tags) {
        getBaseMapper().saveByName(tags);
    }
}




